<!DOCTYPE html>
<html>
<head>
<title>分类商品</title>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<meta name="viewport" id="viewport" content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<meta name="format-detection" content="telephone=no"/>
<link href="/static/webAPP/css/reset.css" rel="stylesheet" type="text/css">
<link href="/static/webAPP/css/discounts.css" rel="stylesheet" type="text/css">
<script src="/static/webAPP/js/jquery-1.8.3.min.js"></script>
<script src="/static/webAPP/js/base.js"></script>
</head>
<body>
<div class="near-box">
      <!--头部开始-->
	  <div class="header">
	  	 <a href="javascript:history.go(-1);" class="left"></a>
	  	 <span class="discountsName"></span>
	  </div>
      <!--头部结束-->
      <a href="/static/webAPP/search.html" class="sousuo_input">
            <img src="/static/webAPP/images/search.png" alt="">
            <input type="text" placeholder="搜索商品名称..." name="" class="sousuo_txt">
        </a>
      <div class="discounts-bigbox discounts_info">

	  </div>
      <!--bigbox结束-->
      <div class="discounts-bottom">
		  
      </div>
      
</div>
<script src="/static/webAPP/lib/mobile/layer.js"></script>
<script type="text/javascript" src="js/scrolltopcontrol.js"></script>
<script>
	$(function(){
		var areaId = sessionStorage.getItem("citycode")
		var discounts = getUrlParam('discounts')
		var discountsName = getUrlParam('discountsName')
		var userName = sessionStorage.getItem("sessionName")
		var user_id = sessionStorage.getItem("sessionUserid")
		$(".discountsName").html(decodeURI(discountsName))
		var tag = ''
		$.ajax({
			type: "get",
						//TODO:
			url: "/app/commodityInfo/getCommodityInfoByCategory/"+ areaId +"/"+ discounts +"/1",
			data: '',
			dataType: "json",
			success: function(mag){
				var data = mag.data
				for (let i = 0; i < data.length; i++) {
                    
                    var tag1 = '<div class="discounts-box1"><a href="/static/webAPP/goods-details.html?id='+ data[i].id +'" class="d-box1-img1"><img src="https://vm.fowotek.com/'+ getSmallUrl(data[i].picture[0].picture_url) +'" alt=""></a><div class="discounts-box2" cid="'+ data[i].id +'" specificationsid="'+ data[i].specifications[0].id +'"><a href="/static/webAPP/goods-details.html?id='+ data[i].id +'" class="d-box2-text1">'+ data[i].commodityName +'</a><span class="d-box2-text2">'+ data[i].specifications[0].specifications +'</span><span class="d-box2-text3">￥'+ data[i].specifications[0].price +'</span><a href="javascript:void(0);" class="d-box2-btn"><img src="/static/webAPP/images/gouwuchee.png"></a></div></div>'

                    
					tag += tag1
				}
				$(".discounts_info").html(tag)
			}
		})



		
		$(document).on('click','.d-box2-btn',function(){
			var id = $(this).closest(".discounts-box2").attr("cid")
			//TODO:加入购物车
			var specificationsid = $(this).closest(".discounts-box2").attr("specificationsid")
			if (userName == null || userName == '' || userName == undefined) {
				layer.open({
					content: '请先去登录账号',
					skin: 'msg',
					time: 2 //2秒后自动关闭
				});
			}else{
				
				$.ajax({
					type: "post",
							//TODO:
					url: "/app/basket/addCommodityToBasket",
					data: {commodity_id: id,commodity_specification_id: specificationsid,count: 1,user_id: user_id},
					dataType: "json",
					success: function(res){
						layer.open({
							content: res.message,
							skin: 'msg',
							time: 2 //2秒后自动关闭
						});
					}
				})
			}
		})


		//下拉加载更多
		var num = 2
		// $(window).scroll(function(){
		$(window).off("scroll").on("scroll",function(){
			var scrollTop = $(this).scrollTop();    
			var scrollHeight = $(document).height();   
			var clientHeight = $(this).height();    
			if(num != 10000){
				// console.log(num)
				if (scrollTop + clientHeight >= scrollHeight) {
					var data
					$.ajax({
						type: "get",
							//TODO:
						url: "/app/commodityInfo/getCommodityInfoByCategory/"+ areaId +"/"+ discounts +"/"+ num,
						data: '',
						async:false,
						dataType: "json",
						success: function(mag){
							data = mag.data
							for (let i = 0; i < data.length; i++) {
								var tag1 = '<div class="discounts-box1"><a href="/static/webAPP/goods-details.html?id='+ data[i].id +'" class="d-box1-img1"><img src="https://vm.fowotek.com/'+ getSmallUrl(data[i].picture[0].picture_url) +'" alt=""></a><div class="discounts-box2" cid="'+ data[i].id +'" specificationsid="'+ data[i].specifications[0].id +'"><a href="/static/webAPP/goods-details.html?id='+ data[i].id +'" class="d-box2-text1">'+ data[i].commodityName +'</a><span class="d-box2-text2">'+ data[i].specifications[0].specifications +'</span><span class="d-box2-text3">￥'+ data[i].specifications[0].price +'</span><a href="javascript:void(0);" class="d-box2-btn"><img src="/static/webAPP/images/gouwuchee.png"></a></div></div>'
								tag += tag1
							}
						}
					})
							if (data.length != 10) {
								num  = 10000
								$(".discounts_info").html(tag + '<div style="text-align: center;color: #ccc;width:100%;float:left">已加载全部商品</div>')
							}else{
								num ++
								$(".discounts_info").html(tag)
							}
				}
			}
		});
	})


	function getUrlParam(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
		var r = window.location.search.substr(1).match(reg); //获取url中"?"符后的字符串并正则匹配
		var context = "";
		if (r != null){
			context = r[2];
			reg = null;
			r = null;
		}
		return context == null || context == "" || context == "undefined" ? "" : context;
	}
</script>
</body>
</html>